package com.top.mall.main.mine

import android.os.Bundle
import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.jetpack.vm.MineViewModel
import kotlinx.android.synthetic.main.activity_mine_mzzm_submit.*
import kotlinx.android.synthetic.main.activity_mine_mzzm_submit.imageViewBack

class MineMzzmSubmitActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        private const val KEY_TITLE = "KEY_TITLE"
        private const val KEY_TYPE = "KEY_TYPE"
        fun open(title: String, type: Int) {
            val bundle = Bundle().apply {
                putString(KEY_TITLE, title)
                putInt(KEY_TYPE, type)
            }
            ActivityUtils.startActivity(bundle, MineMzzmSubmitActivity::class.java)
        }
    }

    private val mTitle: String get() = intent?.extras?.getString(KEY_TITLE) ?: ""
    private val mType: Int get() = intent?.extras?.getInt(KEY_TYPE, 1) ?: 1

    private val mOptionsStateTime: TimePickerView by lazy {
        TimePickerBuilder(this) { date, view ->
            textView1.text = TimeUtils.date2String(date, "yyyy-MM-dd")
        }
            .setType(
                booleanArrayOf(true, true, true, false, false, false)
            )
            .setLabel(("年"), ("月"), ("日"), ("时"), ("分"), ("秒"))
            .isCenterLabel(false)
            .isCyclic(false)
            .build()
    }

    private val mOptionsEndTime: TimePickerView by lazy {
        TimePickerBuilder(this) { date, view ->
            textView2.text = TimeUtils.date2String(date, "yyyy-MM-dd")
        }
            .setType(
                booleanArrayOf(true, true, true, false, false, false)
            )
            .setLabel(("年"), ("月"), ("日"), ("时"), ("分"), ("秒"))
            .isCenterLabel(false)
            .isCyclic(false)
            .build()
    }
    override val layoutResId: Int
        get() = R.layout.activity_mine_mzzm_submit

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineMzzmBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        textViewTitle.text = mTitle

        textView1.setOnClickListener {
            mOptionsStateTime.show()
        }
        textView2.setOnClickListener {
            mOptionsEndTime.show()
        }


        textViewSubmit.setOnClickListener {
            if (editText1.text.isNullOrEmpty()) {
                return@setOnClickListener
            }
            if (editText2.text.isNullOrEmpty()) {
                return@setOnClickListener
            }
            if (textView1.text.isNullOrEmpty()) {
                return@setOnClickListener
            }
            if (textView2.text.isNullOrEmpty()) {
                return@setOnClickListener
            }
            viewModel.submitInvoiceVoidTax(
                mType,
                editText2.text.toString(),
                editText1.text.toString(),
                textView1.text.toString(),
                textView2.text.toString(),
                "http://topmall.icarefz.cn/uploads/20210827/bb696bb15145428b058af227d469c8f1.png"
            )
        }
    }

    override fun initViewMode() {
        viewModel.submitInvoiceVoidTaxLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg)
            finish()
        })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }
}